import 'dart:convert';
import 'dart:developer';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jxchmz/API/LoginApi.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/GetxTools.dart';

class KuCunManager {
  LoginApi loginApi = new LoginApi();
  Dio dio = new Dio();
  var ResponseData;

  // 获取本地token
  configDio() async {

    // 获取本地token
    GetStorage storage = GetStorage();
    String Token =await storage.read("Token");
    // 构造请求头
    dio.options.contentType="application/json";
    dio.options.headers['Authorization'] = "Bearer $Token";
    dio.options.connectTimeout = 5000;
    dio.options.receiveTimeout = 3000;
  }

//  获取仓库列表
  GetCangKu() async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/warehouse/listAll");
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //获取采购订单
  GetPrfOrder(data) async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/order/listAll",queryParameters: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //获取销售订单
  GetSaleOrder(data) async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/salorder/list",queryParameters: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //获取盘点单
  GetInventoryOrderList(data) async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/jxcstkcheck/list",queryParameters: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }
  //提交盘点数据
  UploadInventoryData(data) async {
    //配置dio
    await configDio();
    log("请求参数:${json.encode(data)}");
    try{ ResponseData = await dio
        .post(loginApi.ApiUrl + "/system/jxcstkcheck/updateStatus",data: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }

  //获取调拨单
  GetTransferOrderList(data) async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/transfer/list",queryParameters: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }
  //查询调拨单详情
  // GetTranXiangQing(data) async{
  //   //配置dio
  //   await configDio();
  //   log("请求参数:${json.encode(data)}");
  //   try{ ResponseData = await dio
  //       .get(ApiUrl + "/inventory/GetMaterialList/$data");
  //   if (ResponseData.statusCode == 200) {
  //     // 请求成功
  //     ResponseData =
  //         json.decode(ResponseData.toString());
  //     log("数据返回:$ResponseData");
  //     return ResponseData;
  //   } else {
  //     log("请求失败，状态码:${ResponseData.statusCode}");
  //     GetxTools().ErrorToast("系统异常");
  //     return "";
  //   }
  //   }catch(e){
  //     GetxTools().ErrorToast("系统异常");
  //     return "";
  //   }
  // }
  //提交调拨数据
  UploadTransferData(data) async {
    //配置dio
    await configDio();
    log("请求参数:${json.encode(data)}");
    try{ ResponseData = await dio
        .post(loginApi.ApiUrl + "/system/inventory/updateTransfer",data: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }



//  获取物资类别列表
  GetGoodsType() async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/jxcstkcheck/getAllCategory");
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //  获取物资列表
  GetGoods() async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/material/listAll");
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //获取物料详情
  GetGoodsXiangQing(id)async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/material/list",queryParameters: id);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //  上传物资录入信息
  UploadLuRu(data) async {
    //配置dio
    await configDio();
    try{
      print("请求参数:$data");
      ResponseData = await dio
          .post(loginApi.ApiUrl + "/system/inventory",data: data);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData =
            json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //  查询标签信息
  SearchEPC(data) async {
    //配置dio
    await configDio();
    try{
      print("请求参数:$data");
      ResponseData = await dio
          .get(loginApi.ApiUrl + "/system/inventory/listCode",queryParameters: data);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData =
            json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    }catch(e){
      // GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  ///dio 实现文件上传
   fileUplod(File image) async{
    //配置dio
    await configDio();
    try{
      String path = image.path;
      print("请求参数:$path");
      var name = path.substring(path.lastIndexOf("/") + 1, path.length);
      var suffix = name.substring(name.lastIndexOf(".") + 1, name.length);
      Map<String ,dynamic> map = Map();
      // map["auth"]="12345";
      map["file"] = await MultipartFile.fromFile(path,filename: "$name.png");
      ///通过FormData
      FormData formdata = FormData.fromMap(map);
       ResponseData = await dio.post(loginApi.ApiUrl + "/system/inventory/uploadImages", data: formdata,
        ///这里是发送请求回调函数
        ///[progress] 当前的进度
        ///[total] 总进度
        onSendProgress: (int progress, int total) {
          print("当前进度是 $progress 总进度是 $total");
        },);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData =
            json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }

  //库存查询
  SearchKuCun(data) async {
    //配置dio
    await configDio();
    try{
      print("请求参数:$data");
      ResponseData = await dio
          .get(loginApi.ApiUrl + "/system/inventory/list",queryParameters: data);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData =
            json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }

  //查询出入库数据
  InventoryDataList(data) async {
    //配置dio
    await configDio();
    try {
      ResponseData = await dio.get(loginApi.ApiUrl + "/system/stkio/getNum", queryParameters: data);
      if (ResponseData.statusCode == 200) {
        // 请求成功
        ResponseData = json.decode(ResponseData.toString());
        log("数据返回:$ResponseData");
        return ResponseData;
      } else {
        log("请求失败，状态码:${ResponseData.statusCode}");
        GetxTools().ErrorToast("系统异常");
        return "";
      }
    } catch (e) {
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }
//查询物料类别（分页）
  GetGoodsTypeNoAll(query) async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/category/list",queryParameters: query);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //新增物料类别
  AddGoodsType(data) async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .post(loginApi.ApiUrl + "/system/category",data: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //删除物料类别
  DeleteGoodsType(id) async {
    //配置dio
    await configDio();
    try{ ResponseData = await dio
        .delete(loginApi.ApiUrl + "/system/category/$id");
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //修改物料类别
  UpdateGoodsType(data) async {
    //配置dio
    await configDio();
    print("请求参数:$data");
    try{ ResponseData = await dio
        .put(loginApi.ApiUrl + "/system/category",data: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }

  }
  //获取出入库单
  GetEntryAndOutOrder(data) async {
    //配置dio
    await configDio();
    print("请求参数:$data");
    try{ ResponseData = await dio
        .get(loginApi.ApiUrl + "/system/stkio/list",queryParameters: data);
    if (ResponseData.statusCode == 200) {
      // 请求成功
      ResponseData =
          json.decode(ResponseData.toString());
      log("数据返回:$ResponseData");
      return ResponseData;
    } else {
      log("请求失败，状态码:${ResponseData.statusCode}");
      GetxTools().ErrorToast("系统异常");
      return "";
    }
    }catch(e){
      GetxTools().ErrorToast("系统异常");
      return "";
    }
  }

}

